package com.atguigu.edu.util;

import com.atguigu.edu.common.Constant;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.flink.shaded.guava18.com.google.common.base.CaseFormat;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;


public class JdbcUtil {
    
    public static Connection getPhoenixConnection() {
        
        String driver = Constant.PHOENIX_DRIVER;
        String url = Constant.PHOENIX_URL;
        
        return getJdbcConnection(driver, url, null, null);
    }
    
    private static Connection getJdbcConnection(String driver,
                                                String url,
                                                String user,
                                                String password) {
        // 加载驱动
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        
        try {
            return DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        
    }
    
    public static void closeConnection(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            
        }
    }
    
    public static Connection getMysqlConnection() {
        String driver = Constant.MYSQL_DRIVER;
        String url = Constant.MYSQL_URL;
        String user = "root";
        String password = "aaaaaa";
        return getJdbcConnection(driver, url, user, password);
    }
    // 通过反射获取对象
    public static <T> List<T> queryList(Connection conn,
                                        String querySql,
                                        Object[] arg,
                                        Class<T> tClass,
                                        boolean isUnderLineToCamel) throws Exception {
        ArrayList<T> result = new ArrayList<>();
        // 1.根据链接对象和 sql 语句得到一个预处理语句
        PreparedStatement ps = conn.prepareStatement(querySql);
        // 2.给占位符赋值
        if (arg != null) {
            for (int i = 0; i < arg.length; i++) {
                Object v = arg[i];
                ps.setObject(i + 1, v);
            }
        }
        // 3.执行查询语句
        ResultSet resultSet = ps.executeQuery();
        ResultSetMetaData metaData = resultSet.getMetaData();
        // 4.解析结果并封装
        while (resultSet.next()) {
            // 创建 T 类型的对象
            T t = tClass.newInstance();
            // 读取这一行的每一列, 放在到对象t中
            for (int i = 1; i <= metaData.getColumnCount(); i++) {
                String columnName = metaData.getColumnLabel(i);
                if (isUnderLineToCamel) {
                    //将列名转换为下划线
                     columnName = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, columnName);
                }

                Object v = resultSet.getObject(i);
                BeanUtils.setProperty(t, columnName, v);
            }
    
            result.add(t);
            
        }
        
        return result;
    }

}
